﻿/************ 正则校验类 ************/
namespace WLFramework.Validation
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using WLFramework.Validation;

    [AttributeUsage(AttributeTargets.Property | AttributeTargets.ReturnValue)]
    public class RegularAttribute : ValidationAttribute
    {

        public RegexTypes RegexType { get; set; }
        public RegularAttribute(RegexTypes type) :
            this(type, GetdisplayErrors(type))
        { 
        }

        public RegularAttribute(RegexTypes type,string displayErrors)
            : this(type,displayErrors, "请输入{0}")
        {
        }


        public RegularAttribute(RegexTypes type,string displayErrors, string displayDefault)
        {
            this.RegexType = type;
            this.ErrorMessage = displayErrors;
            this.DefaultMessage = displayDefault;
        }

        public override ValidatePriority Priority
        {
            get
            {
                return ValidatePriority.High;
            }
        }

        public override ValidateResult IsValid(object input, ValidateContext context)
        {
            bool flag = true;
            switch (RegexType)
            {
                case RegexTypes.CHTel:
                    flag = RegexValidation.IsCHTel(input.ToString());
                    break;

                case RegexTypes.CHMobile:
                    flag = RegexValidation.IsCHMobile(input.ToString());
                    break;

                case RegexTypes.Email:
                    flag = RegexValidation.IsEmail(input.ToString());
                    break;

                case RegexTypes.URL:
                    flag = RegexValidation.IsURL(input.ToString());
                    break;

                case RegexTypes.IP:
                    flag = RegexValidation.IsIP(input.ToString());
                    break;

                case RegexTypes.QQ:
                    flag = RegexValidation.IsQQ(input.ToString());
                    break;

                case RegexTypes.PostCode:
                    flag = RegexValidation.IsPostCode(input.ToString());
                    break;

                case RegexTypes.CHZN:
                    flag = RegexValidation.IsCHZN(input.ToString());
                    break;

                case RegexTypes.NormalChar:
                    flag = RegexValidation.IsNormalChar(input.ToString());
                    break;

                case RegexTypes.ENZN:
                    flag = RegexValidation.IsENZN(input.ToString());
                    break;

                default:
                    break;
            }

            return flag ? ValidateResult.Success : new ValidateResult(this.FormatErrorMessage(context.DisplayName));
        }


        private static string GetdisplayErrors(RegexTypes type)
        {
            switch (type)
            {
                case RegexTypes.CHTel:
                    return "{0}格式不正确！(正确格式:0411-39618938)";
                case RegexTypes.CHMobile:
                    return "{0}格式不正确！(正确格式:18642856503)";
                case RegexTypes.Email:
                    return "{0}格式不正确！(正确格式:admin@xk-soft.com)";
                case RegexTypes.URL:
                    return "{0}格式不正确！(正确格式:http://www.xk-soft.com)";
                case RegexTypes.IP:
                    return "{0}格式不正确！(正确格式:192.168.0.1)";
                case RegexTypes.QQ:
                    return "{0}格式不正确！(正确格式:56267698)";
                case RegexTypes.PostCode:
                    return "{0}格式不正确！(正确格式:116000)";
                case RegexTypes.CHZN:
                    return "{0}必须是中文字符！";
                case RegexTypes.NormalChar:
                    return "{0}只允许为英文字母或数字开头，可包含有下划线，但不能以下划线结尾！";
                case RegexTypes.ENZN:
                    return "{0}必须是英文字符！";
                default :
                    return "";
            }
        }

        public enum RegexTypes
        { 
            CHTel = 1 ,             // 中国电话
            CHMobile = 2 ,          // 中国手机
            Email = 3 ,             // 邮箱
            URL = 4 ,               // URL
            IP = 5 ,                // IP
            QQ = 6 ,                // QQ号
            PostCode = 7 ,          // 邮编
            CHZN = 8 ,              // 中文
            NormalChar = 9 ,        // 正常字符
            ENZN = 10 ,             // 英文
        }
    }
}
